package cn.stylefeng.guns.modular.recy.controller;
/*
 */

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.stylefeng.roses.core.base.controller.*;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.*;
import cn.stylefeng.guns.modular.recy.service.*;
import org.springframework.beans.factory.annotation.*;
import cn.stylefeng.roses.core.reqres.response.*;
import cn.stylefeng.guns.modular.recy.entity.*;
import java.io.*;
import cn.stylefeng.guns.core.common.page.*;
import com.baomidou.mybatisplus.core.metadata.*;
import com.baomidou.mybatisplus.core.conditions.*;
import cn.stylefeng.guns.modular.recy.vo.*;
import cn.stylefeng.guns.modular.recy.utils.*;
import cn.hutool.core.bean.*;
import java.util.stream.*;
import cn.stylefeng.guns.modular.recy.warpper.*;
import com.baomidou.mybatisplus.core.conditions.query.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.*;
import java.util.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;

import javax.naming.ldap.Control;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping({ "/wasteType" })
public class WasteTypeController extends BaseController
{
    private String PREFIX;
    @Autowired
    private WasteTypeService wasteTypeService;
    
    public WasteTypeController() {
        this.PREFIX = "/modular/recy/wasteType/";
    }
    
    @RequestMapping({ "" })
    public String index() {
        return this.PREFIX + "wasteType.html";
    }
    
    @RequestMapping({ "/add" })
    public String wasteTypeAdd() {
        return this.PREFIX + "wasteType_add.html";
    }
    
    @RequestMapping({ "/edit" })
    public String wasteTypeUpdate(@RequestParam("id") final String id) {
        return this.PREFIX + "wasteType_edit.html";
    }
    
    @RequestMapping({ "/detail/{id}" })
    @ResponseBody
    public ResponseData detail(@PathVariable final String id) {
        final WasteType wasteType = (WasteType)this.wasteTypeService.getById((Serializable)id);
        return (ResponseData)ResponseData.success(wasteType);
    }
    
    @RequestMapping({ "/list" })
    @ResponseBody
    public Object list(final WasteType wasteTypeQuery) {
        final QueryWrapper<WasteType> wasteTypeQueryWrapper = ControllerUtil.createQuery(wasteTypeQuery, this.getHttpServletRequest());
        final Page<WasteType> page = (Page<WasteType>)LayuiPageFactory.defaultPage();
        final IPage<WasteType> page2 = (IPage<WasteType>)this.wasteTypeService.page((IPage)page, (Wrapper)wasteTypeQueryWrapper);
        final List<WasteTypeVO> wasteTypeVOList = EntityBind.bind(page2.getRecords(), WasteTypeVO.class);
        final List<Map<String, Object>> list = wasteTypeVOList.stream().map(wasteType -> BeanUtil.beanToMap(wasteType)).collect(Collectors.toList());
        page2.setRecords((List)new WasteTypeWrapper(list).wrap());
        return LayuiPageFactory.createPageInfo(page2);
    }
    
    @RequestMapping({ "/addItem" })
    @ResponseBody
    public Object add(final WasteType wasteType) {
        final HttpServletRequest request = this.getHttpServletRequest();
        final StandardServletMultipartResolver resolver = new StandardServletMultipartResolver();
        final MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(request);
        final MultipartFile file = multipartRequest.getFile("file");
        try {
            final String name = file.getOriginalFilename();
            final InputStream inputStream = file.getInputStream();
            String fileName = ControllerUtil.saveFile("/upload/brand", inputStream, name);
            wasteType.setIcon(fileName);

        }
        catch (IOException e) {
            e.printStackTrace();
        }
        this.wasteTypeService.save(wasteType);
        return WasteTypeController.SUCCESS_TIP;
    }
    
    @RequestMapping({ "/delete" })
    @ResponseBody
    public Object delete(@RequestParam final String id) {
        this.wasteTypeService.removeById((Serializable)id);
        return WasteTypeController.SUCCESS_TIP;
    }
    
    @RequestMapping({ "/editItem" })
    @ResponseBody
    public Object update(final WasteType wasteType) {
        final HttpServletRequest request = this.getHttpServletRequest();
        final String header = request.getHeader("content-type");
        if (StringUtils.containsIgnoreCase((CharSequence)header, (CharSequence)"multipart/form-data")) {
            final StandardServletMultipartResolver resolver = new StandardServletMultipartResolver();
            final MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(request);
            final Map<String, MultipartFile> fileMap = (Map<String, MultipartFile>)multipartRequest.getFileMap();
            final boolean file1 = fileMap.containsKey("file");
            if (file1) {
                final MultipartFile file2 = multipartRequest.getFile("file");
                try {
                    final String name = file2.getOriginalFilename();
                    final InputStream inputStream = file2.getInputStream();
                    String fileName = ControllerUtil.saveFile("/upload/brand", inputStream, name);
                    wasteType.setIcon(fileName);
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        this.wasteTypeService.updateById(wasteType);
        return WasteTypeController.SUCCESS_TIP;
    }
    
    @RequestMapping(value = { "/delItems" }, method = { RequestMethod.POST })
    @ResponseBody
    public Object delItem(@RequestBody final String[] ids) {
        this.wasteTypeService.removeByIds((Collection)Arrays.asList(ids));
        return WasteTypeController.SUCCESS_TIP;
    }
    
    @RequestMapping({ "/show" })
    public Object show(final String id, final Model model) {
        final WasteType wasteType = (WasteType)this.wasteTypeService.getById((Serializable)id);
        final Map map = BeanUtil.beanToMap(wasteType);
        model.addAttribute("wasteType", new WasteTypeWrapper(map).wrap());
        return this.PREFIX + "wasteType_show.html";
    }
}
